/**
 * Created by Administrator on 2016/12/13.
 */
function DGrid(ele,opt) {
    this.options={
        trip : false,//隔行变色.
        Edit : false //实时编辑
    };
    if(opt){
        for(var p in opt){
            this.options[p]=opt[p];
        }
        // console.log(this.options);
    }
    if(!ele){
        console.log('错误的对象');
    }
 this.obj=ele;
    if(this.options.HCBox){
        this.addHCBox(this.options.HCBox);
    }


}
// 全选
DGrid.prototype.checkAll=function () {
    var input = this.obj.find('input').each(function (index,ele) {
        ele.checked=true;
    })
};
//全不选
DGrid.prototype.checkUnAll=function () {
    var input = this.obj.find('input').each(function (index,ele) {
        ele.checked=false;
    })
}
//反选
DGrid.prototype.checkContrary=function () {
    var input = this.obj.find('input').each(function (index,ele) {
        if(ele.checked){
            ele.checked=false;
        }else {
            ele.checked=true;
        }
    })
}
//删除所选择的列
DGrid.prototype.delChecked=function () {
    var input = this.obj.find('input').each(function (index,ele) {
        if(ele.checked&&$(ele).parent()[0].tagName=='TD'){
            $(ele).parents('tr').remove();
        }
    })
}
//可编辑表格
DGrid.prototype.openEdit=function () {
    this.obj.on('click','',function (e) {
        // console.log(e.target);
        var obj;
        // console.log($(e.target).parent()[0].tagName=="TD"&&!$(e.target).parent().hasClass('no_edit'));
        if(($(e.target).parent()[0].tagName=="TD"&&!$(e.target).parent().hasClass('no_edit'))||(e.target.tagName=="TD"&&!$(e.target).hasClass('no_edit'))){
            // 失去焦点动作
            if($(e.target).parent()[0].tagName=="TD"){
                obj =$(e.target).parent();
            }else {
                obj = $(e.target)
            }
            function DGrid_input_lostFocus() {
                $('.DGrid_edit_input').each(function (index,ele) {
                    var valEle=$(ele);
                    // console.log(valEle.val());
                    valEle.parents('td').find('span').text(valEle.val());
                    valEle.css("display","none");
                });
            }
            DGrid_input_lostFocus();
            // $('.DGrid_edit_input').css("display","none");
            // console.log(obj.find(".DGrid_edit_input").length);
            // 获得焦点显示编辑框
            if(obj.find(".DGrid_edit_input").length==0){
                obj.css("display","relative").append($('<input type="text" class="DGrid_edit_input">'));
                //表格中内容初始化到编辑框中
                $('.DGrid_edit_input').each(function (index,ele) {
                    var valEle=$(ele);
                    // console.log(valEle.parents('td').text());
                    valEle.val(valEle.parents('td').text());
                });
                //文本框获得焦点
                obj.find('.DGrid_edit_input').focus();
            }else {
                // console.log('block');
                obj.find(".DGrid_edit_input").css("display","block");
                obj.find('.DGrid_edit_input').focus();

            }
            $(document.body).on('click',function (e) {
                // console.log($(e.target).parents('tr').find("td"));
                if(e.target.tagName!="TD"&&!$(e.target).hasClass('DGrid_edit_input')&&$(e.target).parent()[0].tagName!='TD'){
                    DGrid_input_lostFocus();
                }

            })
        }
    })
}
// 添加列头input框
DGrid.prototype.addHCBox=function () {
    var self = this;
    var tb = this.obj.find('tbody').find('tr');
    var th = this.obj.find('thead').find('tr');
    var className = th.children().eq(1).attr('class');
    th.prepend('<th class="'+className+'"><input type="checkbox"></th>',th.find('th:first-child'));
    // 头部全选
    th.find('th:first-child').find('input').on('click',function () {
        if(this.checked){
            self.checkAll();
        }else {
            self.checkUnAll();
        }
    });
    // console.log(tb.find('tr'));
    tb.find('td:first-child').before('<td class="no_edit"><input type="checkbox" class="DGrid_head_checkbox"></td>');
    //点击td影响checkBox
    $('.DGrid_head_checkbox').parent('td').on('click',function (e) {
        if(e.target.tagName!='INPUT'){
            checkbox=$(this).find('.DGrid_head_checkbox')[0].click();
        }

    })
    // checkbox
    this.obj.on('click',function (e) {
        if(e.target.tagName=="INPUT"&&$(e.target).hasClass('DGrid_head_checkbox')){
            // 兼容ie8
           if([].every){
               if([].every.call($('.DGrid_head_checkbox'),function (ele, index) {
                       return ele.checked;
                   })){
                   th.find('th:first-child').find('input').attr("checked",true);
               }else {
                   th.find('th:first-child').find('input').attr("checked",false);
               }
           }else {
               var count=0;
               $('.DGrid_head_checkbox').each(function (index, ele) {
                   if(ele.checked){
                       count++;
                   }
               })
               if($('.DGrid_head_checkbox').length==count){
                   th.find('th:first-child').find('input').attr("checked",true);
               }else {
                   th.find('th:first-child').find('input').attr("checked",false);
               }

           }



        }
    });
       
};
//可拖拽宽度
DGrid.prototype.dragTr = function () {
    var th = $(this.obj).find('thead th');
    th.each(function (index, ele) {
        if (index < th.length - 1)
            $(ele).append($('<div class="dragTr_box"></div>')).css("z-index", 999 - index);
    });
    $(this.obj).on('mousedown.dragBox', function (e) {
        var cx = e.clientX;
        var tag = $(e.target);
        if (e.target.tagName == "DIV" && tag.hasClass("dragTr_box")) {
            $(document).on('selectstart.DGrid', function () {
                return false;
            });
            //生成标尺
            var height = $(this).css("height");
            var staff = $('<div class="DGrid_staff"></div>').css("height", height);
            $('.DGrid_table_content').append(staff);
            var tableLeft = $(this).offset().left;
            var bodyScrollLeft = $(document.body).scrollLeft()
            $('.DGrid_staff').css('left', e.clientX - $(this).offset().left + bodyScrollLeft);
            var moveX, width;
            $(document.body).on('mousemove.dragBoxDown', function (e) {
                moveX = e.clientX - cx;
                width = tag.parent("th").css("width");
                $('.DGrid_staff').css('left', e.clientX - tableLeft + bodyScrollLeft);
            });
            $(document.body).on('mouseup.dragBoxUp', function () {
                tag.parent("th").css("width", parseInt(width) + moveX + 'px');
                $(document.body).unbind(".dragBoxDown .dragBoxUp");
                $(document).unbind(".DGrid");
                $('.DGrid_staff').remove()

            })
        }

    })
}
